import {
    updateStorage
} from './tools'
import request from '../utils/request'

let address = localStorage.getItem('address')
try {
    address = JSON.parse(address) || {}
} catch (err) {
    address = {}
}

export default {
    // 启用命名空间
    namespaced: true,
    state: {
        address,
    },
    getters: {
        // defaultAddress(state) {
        //     return state.address.filter(item => {
        //         return item.Isdefault == 1
        //     })[0]
        // }
    },
    mutations: {
        addressGet(state, payload) {
            state.address = payload
            updateStorage('address', payload)
        },
        addressAdd(state, payload) {
            state.address.push(payload);
        },
        addressDel(state, payload) {
            state.address = state.address.filter(item => {
                return item.Addressid != payload;
            })
        },
        // addressExit(state) {
        //     state.address.address = {}
        //     localStorage.removeItem('address')
        // }
    },
    actions: {
        async getAddress(context, payload) {
            const {
                data
            } = await request.post(
                "getAddress", {
                username: context.rootState.user.userInfo.username,
            }, {
                headers: {
                    token: context.rootState.user.userInfo.token,
                },
            }
            )
            context.commit('addressGet', data.data)
            return data
        },
        async addAddress(context, payload) {
            console.log(payload);
            const {
                name,
                tel,
                province,
                city,
                county,
                addressDetail,
                isDefault,
                postalCode,
                Addressid
            } = payload;
            console.log('addressid=', Addressid);
            const {
                data
            } = await request.post(
                "addAddress", {
                username: context.rootState.user.userInfo.username,
                name: name,
                phonenum: tel,
                address: province + city + county,
                addressDetail,
                postalCode,
                isDefault: isDefault ? 1 : 0,
                Addressid
            }, {
                headers: {
                    token: context.rootState.user.userInfo.token,
                },
            }
            )
            const addObj = {
                Address: province + city + county,
                AddressDetail: addressDetail,
                Addressid: 1,
                Isdefault: isDefault ? 1 : 0,
                Name: name,
                Phonenum: tel,
                PostalCode: postalCode,
                Username: context.rootState.user.userInfo.username,

            }
            console.log(isDefault);
            console.log(addObj);
            context.commit('addressAdd', addObj)
            return data
        },
        async delAddress(context, payload) {
            const {
                data
            } = await request.post(
                "delAddress", {
                username: context.rootState.user.userInfo.username,
                addressid: payload
            }, {
                headers: {
                    token: context.rootState.user.userInfo.token,
                },
            }
            )
            context.commit('addressDel', payload)
            return data
        }
    }
}